/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel.annotation;

import java.util.ArrayList;
import java.util.List;

import com.prefabware.commons.CollectionUtil;
import com.prefabware.commons.collection.Filter;
import com.prefabware.commons.collection.Gettable;

public class JAnnotations implements JAnnotatable{
	private List<JAnnotation> annotations;

	public JAnnotations() {
		super();
		this.annotations = new ArrayList<JAnnotation>();
	}

	@Override
	public void add(JAnnotation element) {
		annotations.add(element);
	}

	@Override
	public List<JAnnotation> getAnnotations() {
		return annotations;
	}
	/**
	 * @param annotationClass
	 * @return the {@link JAnnotation} oft the given annotationClass, if one exists. <br> 
	 *         returns null if none exists with the qualifiedName  
	 */
	@Override
	public JAnnotation getAnnotation(final JAnnotationClass annotationClass) {
		Filter<? super JAnnotation> annotationClassFilter = new Filter<JAnnotation>() {

			@Override
			public boolean includes(JAnnotation value) {
				if (value.getAnnotationClass().equals(annotationClass)) {
					return true;
				} else {
					return false;
				}
			}
		};
		return CollectionUtil.first(this.annotations, annotationClassFilter);
	}
	/**
	 * @param qualifiedName
	 * @return the {@link JAnnotation} with the qualifiedName, if one exists. <br> 
	 *         returns null if none exists with the qualifiedName  
	 */
	@Override
	public JAnnotation getAnnotation(final String qualifiedName) {
		Filter<? super JAnnotation> annotationClassFilter = new Filter<JAnnotation>() {
			
			@Override
			public boolean includes(JAnnotation value) {
				if (value.getAnnotationClass().getQualifiedName().getName().equals(qualifiedName)) {
					return true;
				} else {
					return false;
				}
			}
		};
		return CollectionUtil.first(this.annotations, annotationClassFilter);
	}
	@Override
	public boolean isAnnotationPresent(JAnnotationClass anClass) {
		Gettable<JAnnotationClass, JAnnotation> gettable = new Gettable<JAnnotationClass, JAnnotation>() {

			@Override
			public JAnnotationClass get(JAnnotation from) {
				if (from==null) {
					return null;
				}
				return from.getAnnotationClass();
			}
		};
		return CollectionUtil.getList(gettable, this.annotations).contains(anClass);
	}
	@Override
	public String toString() {
		return "JAnnotations [annotations=" + annotations + "]";
	}
	
}
